#ifndef LOGGER_H
#define LOGGER_H

#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <thread>
#include <vector>
#include <unordered_map>
#include <memory>
#include <fstream>   
#include <sstream>   
#include <string>

using namespace std;

namespace loger { 
    enum class LogLevel; 
    class Appender;
    class LoggerManager;
    class Formatter;
    class Logger;
    /*----------inherit---------------*/
    class ConsoleAppender;
    class FileAppender;
    class SimpleFormatter;
}

namespace loger
{

enum class LogLevel { 
    TRACE, //最详细调试信息
    DEBUG, //调试信息
    INFO,  //重要的运行状态信息
    WARN,  //潜在的问题或异常情况
    ERROR, //错误信息
    FATAL, //致命的错误信息
    NUM_LOG_LEVELS, //占位符 标识有多少日志级别总数
    };
    

class Appender {    /*输出目标*/
public:
    virtual ~Appender() = default;
    virtual void write(const std::string& message) = 0;
};

class Formatter { /*格式化器*/
    public:
    virtual string format(LogLevel level, const std::string& message) = 0;
};  

class Logger {
public:
// 默认构造函数
Logger() : formatter_(nullptr), logLevel_(LogLevel::INFO) {}
Logger(std::shared_ptr<Formatter> formatter, std::vector<std::shared_ptr<Appender>> appenders)
        :formatter_(formatter), appenders_(appenders), logLevel_(LogLevel::INFO) {}
        
void setLogLevel(LogLevel level);
  
void log(LogLevel level, const std::string& message);
private:
    std::shared_ptr<Formatter> formatter_;
    std::vector<std::shared_ptr<Appender>> appenders_;
    LogLevel logLevel_;
};



class LoggerManager { /*全局管理器*/
public:
    static Logger& getLogger(const std::string& name);
private:
    static std::unordered_map<std::string, Logger> loggers;
    // std::unordered_map<std::string, Logger> LoggerManager::loggers;
    // 初始化静态成员变量
};



/*----------------------------------inherit----------------------------------*/
class ConsoleAppender : public Appender {
    public:
        void write(const std::string& message) override {
            std::cout << message << std::endl;
        }
    };

class FileAppender : public Appender {
    public:
    FileAppender(const std::string& filePath) {
        file_.open(filePath, std::ios::out | std::ios::app);
        }
        
    ~FileAppender() {
        if (file_.is_open()) {
            file_.close();
            }
        }
        
    void write(const std::string& message) override {
        if (file_.is_open()) {
            file_ << message << std::endl;
            }
        }
        
private:
        std::ofstream file_;
};

class SimpleFormatter : public Formatter {
public:
    std::string format(LogLevel level, const std::string& message) override {
    std::ostringstream oss;
    oss << "[" << getLogLevelString(level) << "] " << message;
        return oss.str();
        }
    
private:
    std::string getLogLevelString(LogLevel level) {
        switch (level) {
            case LogLevel::TRACE: return "TRACE";
            case LogLevel::DEBUG: return "DEBUG";
            case LogLevel::INFO: return "INFO";
            case LogLevel::WARN: return "WARN";
            case LogLevel::ERROR: return "ERROR";
            case LogLevel::FATAL: return "FATAL";
            default: return "UNKNOWN";
            }
        }
    
};



// 初始化静态成员变量
// std::unordered_map<std::string, Logger> LoggerManager::loggers;


}
#endif 